import request from '@/utils/request'


export function createWorkflow(data) {
  return request({
    url: '/app',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: data
  })
}

export function reflectWorkflow(reflect) {
  return request({
    url: `/app/workflow`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: reflect
  })
}

export function saveWorkflowAsApp(workflow) {
  return request({
    url: `/app/add`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: workflow
  })
}

export function getAppDetail(app_id) {
  return request({
    url: `/app/${app_id}`,
    method: 'get'
  })
}

export function deleteApp(app_id) {
  return request({
    url: `/app/${app_id}`,
    method: 'delete'
  })
}

export function updateApp(app_detail) {
  return request({
    url: `/app/update`,
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: app_detail
  })
}

export function updateBasicApp(app_basic) {
  return request({
    url: `/app/update`,
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: app_basic
  })
}

export function getAppList(page, pageSize, creator_id, search_query) {
  return request({
    url: '/app',
    method: 'get',
    params: { page, pageSize, creator_id, search_query }
  })
}

// 获取当前登录用户创建的应用列表
export function getAppListByUserId(user_id) {
  return request({
    url: `/app/list/${user_id}`,
    method: 'get'
  })
}

export function getAppById(id) {
  return request({
    url: `/app/${id}`,
    method: 'get'
  })
}

export function deleteByCreatorId(creator_id) {
  return request({
    url: `/app/deleteByCreatorId/${creator_id}`,
    method: 'delete'
  })
}

export function deleteUserPublished(creator_id) {
  return request({
    url: `/app/delete_user_published/${creator_id}`,
    method: 'delete'
  })
}

export function deleteUserStars(user_id) {
  return request({
    url: `/app/delete_user_stars/${user_id}`,
    method: 'delete'
  })
}

// 根据设置的提示词给出大模型生成的用户名称或者描述
export function generateAppInfo(data) {
  return request({
    url: '/generate_info',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: data
  })
}

export function importTemplate(data) {
  return request({
    url: '/app/import_template',
    method: 'POST',
    data: data
  })
}

export function uploadAppIcon(data, app_id) {
  return request({
    url: '/app/upload_icon',
    method: 'post',
    data: data, // 传递 FormData 数据
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    params: {
      app_id: app_id
    }
  })
}

export function getIconByAppId(app_id) {
  return request({
    url: `/app/get_icon_by_app_id`,
    method: 'get',
    params: {
      app_id: app_id
    },
    responseType: 'blob' // 设置响应类型为blob，因为我们要处理的是图片数据
  });
}

export function getAppsByToolId(tool_id) {
  return request({
    url: `/app/getAppsByToolId`,
    method: 'get',
    params: {
      tool_id: tool_id
    },
  })
}

export function getAppsByIds(ids){
  return request({
    url: '/app/getByIds',
    method: 'get',
    params: {
      ids: ids
    },
  })
}